<template>
    <view style="padding: 15px;">
        <u-form ref="validateFormRef" :model="validateForm.value" :label-width="200">
            <u-form-item label="新的登录账号" prop="username" required>
                <u-input v-model="validateForm.value.username" placeholder="请输入新的登录账号"/>
            </u-form-item>
            <u-form-item label="确认登录账号" prop="confirmUsername" required>
                <u-input v-model="validateForm.value.confirmUsername" placeholder="请输入确认登录账号"/>
            </u-form-item>
        </u-form>

        <u-button type="error" @click="submit">确认修改</u-button>
    </view>
</template>

<script>

    import {SysUserService} from "@/dev/services/sys-user.service";
    import {CommonUtil} from "@/dev/core/utils/common.util";
    import {LocalStorageUtil} from "@/dev/core/utils/local-storage.util";
    import {CommonService} from "@/dev/services/common.service";
    import {toValue} from "@/dev/core/utils/filters.util";

    export default {
        data() {
            return {
                validateForm: {
                    value: {
                        id: this.globalVariable.userInfo.id,
                        username: '',
                        confirmUsername: '',
                        allowAlterFields: this.globalVariable.userInfo.allowAlterFields.replace('username', 'USERNAME已修改')
                    },
                    rules: {
                        username: [
                            {required: true, message: '请输入新的登录账号!', trigger: ['change', 'blur']},
                            {pattern: /^(?=.*\S).+$/, message: '请输入新的登录账号!', trigger: ['change', 'blur']},
                            {
                                trigger: ['blur'],
                                asyncValidator: (rule, value, callback) => {
                                    new CommonService().uniqueCheckByFields({
                                        'tableName': 'sys_user',
                                        'fieldList': [
                                            {'fieldName': 'username', 'fieldValue': value}
                                        ],
                                        'excludeId': this.globalVariable.userInfo.id
                                    }).then(result => {
                                        if (result.status === "fail") {
                                            callback(new Error('该登录账号无法使用！'));
                                        } else {
                                            callback();
                                        }
                                    });
                                }
                            }
                        ],
                        confirmUsername: [
                            {required: true, message: '请输入确认登录账号!', trigger: ['change', 'blur']},
                            {pattern: /^(?=.*\S).+$/, message: '请输入确认登录账号!', trigger: ['change', 'blur']},
                            {
                                trigger: ['blur'],
                                asyncValidator: (rule, value, callback) => {
                                    console.log(rule, value, callback)
                                    if (value !== this.validateForm.value.username) {
                                        callback(new Error('两次输入登录账号不一致！'))
                                    } else {
                                        callback()
                                    }
                                }
                            }
                        ]
                    }
                }
            }
        },
        onLoad(options) {
            const _self = this
        },
        methods: {
            submit() {
                const _self = this;
                this.$refs.validateFormRef.validate(valid => {
                    if (valid) {
                        console.log('验证通过', _self.validateForm.value);
                        if (toValue('globalVariable.userInfo.allowAlterFields', this.globalVariable, '').indexOf('username') === -1) {
                            return;
                        }
                        CommonUtil.alert({
                            title: '提示！',
                            content: '确认提交吗？',
                            showCancel: true, // 是否显示取消按钮
                            success: function (res) {
                                if (res.cancel) { // 点击取消,默认隐藏弹框
                                } else { // 点击确定
                                    new SysUserService().updateSysUser(_self.validateForm.value).then(result => {
                                        Object.assign(_self.globalVariable.userInfo, CommonUtil.deleteNullAttrInObject(result));
                                        LocalStorageUtil.setItem("userInfo", _self.globalVariable.userInfo);
                                        CommonUtil.toastNavigateBack('修改成功')
                                    })
                                }
                            }
                        });
                    } else {
                        console.log('验证失败');
                    }
                });
            }
        }
    }
</script>

<style lang="scss" scoped>
</style>
